import formJs from "./regexDetail.js"; //新增弹框配置项
// import publicForm from "@/components/public/up-form.vue"; //弹框form
import publicForm from "@/components/public/up-form.vue"; //弹框form
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";

import {
    saveOutsourcedtesting, //保存
    getOutsourcedtestingByFid, //详情
} from "@/api/request.js";
export default {

    data() {
        return {
            isSave: true,
            primaryInfo: null,
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            positionData: [],
            positionData1: [],
            selected: [], //多选人
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            additionalObj: formJs.additionalObj,
            isEdit: false,
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //必传
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fCommissiondates: this.common.day(0), //委托日期
                fCommissionname: "", //委托人
                fCommissionid: "", //委托人id
                fUnitname: "", //接收委托单位名称
                fUnitid: "", //接收委托单位id
                fOrdernumber: "", //委托单号
                fFileurl: "", //委托单附件
                fNote: "", //备注
                fCompanyid: "",
                fChecktype: "",
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selectPostionObj: [],
            types: [],
            // 材料弹框
            materialRow: {},

            fCompanynames: null,
            fCompanyids: null,
            fFileurllist: [], //附件
            primaryInfo: null,
        };
    },
    components: {
        publicForm,
        FormLyout
    },

    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.isSave = true
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser

            // this.formData.fProjectid = data[0].projectId;
            // this.formData.fProjectname = data[0].projectName;


        }, true);

        if (this.primaryInfo.status == 1) { //编辑
            // currentRow
            this.formData.fId = this.primaryInfo.data.fId//按钮 
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[6].uploadSelect = true
        } else if (this.primaryInfo.status == 7) { //查看
            this.formData.fId = this.primaryInfo.data.fId//按钮 
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.isSave = false
            this.formObj.map((item) => {
                item.disabled = true;
            });

            this.formObj[6].uploadSelect = false
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fManufacturername") { //生产厂家
                this.formData.fManufacturername = data.label; //生产厂家
                this.formData.fManufacturerid = data.value //生产厂家id
            } else if (b == "fSuppliername") { //供应商名称
                this.formData.fSuppliername = data.label; //供应商
                this.formData.fSupplierid = data.value //供应商id
            } else if (b == 'fSpecification') {
                this.formData.fSpecification = data.value //供应商id
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fFileurllist.length; index++) {
                if (file.uid == this.fFileurllist[index].uid) {
                    if (this.fFileurllist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].atta.fId,
                        });
                    }
                    this.fFileurllist.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getOutsourcedtestingByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClOutsourcedtesting
                    this.formData = data;
                    this.formData.fFileurl = data.fFileurl;
                    this.fFileurllist = res.data.fFileurllist;
                    if (this.primaryInfo.status == 1) {
                        this.formData.recordfiles = [];
                        // 委托人
                        if (data.fCommissionid == null || data.fCommissionid == "" || data.fCommissionname == null || data.fCommissionname == "") {
                            this.formData.fCommissionid = this.userInfo.id;
                            this.formData.fCommissionname = this.userInfo.userName;
                        }
                        // 委托日期
                        if (data.fCommissiondates == null || data.fCommissiondates == "") {
                            this.formData.fCommissiondates = this.common.day(0)
                        }
                    }

                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error({
                        content: res.msg,
                        duration: 1000
                    })
                }

            });
        },
        yj_saveForm() {
            this.formData.userId = this.userInfo.id; //当前登录人id
            this.formData.userName = this.userInfo.userName; //当前登录人名称
            this.formData.fCommissionid = this.userInfo.id;
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(obj) {
            var data = {
                fId: obj.fId, //必传
                userId: obj.userId, //当前登录人id
                userName: obj.userName, //当前登录人姓名
                recordfiles: obj.recordfiles, //删除文件id集合
                fCommissiondates: obj.fCommissiondates, //委托日期
                fCommissionname: obj.fCommissionname, //委托人
                fCommissionid: obj.fCommissionid, //委托人id
                fUnitname: obj.fUnitname, //接收委托单位名称
                fUnitid: obj.fUnitid, //接收委托单位id
                fOrdernumber: obj.fOrdernumber, //委托单号
                fFileurl: obj.fFileurl, //委托单附件
                fNote: obj.fNote, //备注
                fCompanyid: obj.fCompanyid,
                fChecktype: obj.fChecktype,
            }
            this.loading = true;
            this.isLock = true;
            saveOutsourcedtesting(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = true;

                        this.$router.push({
                            path: "/outsourcedManage/outsourcedSample",

                        });
                        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");

                        this.isLock = false
                    } else {
                        this.loading = true;
                        this.isLock = false
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        console.log("接口报错");
                    }
                })

        },
    },
};